<?php

namespace LowCode\Support;

class Js
{
    /**
     * @param mixed $data
     * @param int $flags
     * @param int $depth
     * @return HelpJs
     */
    public static function from(mixed $data, int $flags = 0, int $depth = 512): HelpJs
    {
        return HelpJs::from($data,$flags,$depth);
    }

    /**
     * window对象
     * @param string $key
     * @return string
     */
    public static function window(string $key = ""): string
    {
        return $key?"window.{$key}":"window";
    }

    /**
     * 当前对象
     * @param string $key
     * @return string
     */
    public static function this(string $key = ""): string
    {
        return $key?"this.{$key}":"this";
    }


    /**
     * 当前对象
     * @param string $key
     * @return string
     */
    public static function state(string $key = ""): string
    {
        return $key?"this.state.{$key}":"this.state";
    }

    /**
     * 当前对象 $refs 对象
     * @param string $key
     * @return string
     */
    public static function refs(string $key = ""): string
    {
        if ($key){
            return self::this("\$refs.{$key}");
        }else{
            return self::this("\$refs");
        }
    }

    /**
     * $el 元素节点
     * @return string
     */
    public static function el(): string
    {
        return self::this("\$el");
    }

    /**
     * libs对象
     * @param string $value
     * @return string
     */
    public static function libs(string $value = ""): string
    {
        if ($value){
            return self::this("\$libs.{$value}");
        }else{
            return self::this("\$libs");
        }
    }


    /**
     * 路由router对象
     * @return string
     */
    public static function router(): string
    {
        return self::this("\$router");
    }

    /**
     * 路由route对象
     * @return string
     */
    public static function route(): string
    {
        return self::this("\$route");
    }

    /**
     * window.$message对象
     * @return string
     */
    public static function message(): string
    {
        return self::window("\$message");
    }

    /**
     * 函数脚本
     * @param string $script
     * @return string
     */
    public static function func(string $script = ""): string
    {
        return func($script);
    }

    public static function downloadFromCanvasRef(string $refKey, string $filename = "QRCode.png"): string
    {
        $canvasRef = Js::refs("$refKey.canvasRef");
        return "() => { this.\$libs.downloadFromCanvas($canvasRef, '$filename') }";
    }

}
